package edu.xpu.project.user.controller;

import java.io.InputStream;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import edu.xpu.project.excel.help.ParseExcel;
import edu.xpu.project.user.entity.CourseType;
import edu.xpu.project.user.entity.ProjectUser;
import edu.xpu.project.user.entity.ScoreEntity;
import edu.xpu.project.user.entity.ScoreItem;
import edu.xpu.project.user.service.CourseTypeService;
import edu.xpu.project.user.service.ProjectUserService;
import edu.xpu.project.user.service.ScoreEntityService;


@Controller
@RequestMapping(value="/score")
public class ScoreCourseController {
	
	@Autowired
	private ProjectUserService projectUserService;
	@Autowired
	private ScoreEntityService entityService;
	@Autowired
	private CourseTypeService courseTypeService;
	
	private static Set<String> randSet = new CopyOnWriteArraySet<String>();	
	
	/**
	 * @describe 导入学生成绩表
	 * @date 2017年5月21日
	 * @author liukang
	 */
	@ResponseBody
	@RequestMapping(value="/excelImport")
	public String excelImport(@RequestParam(value="file") MultipartFile multipartFile, @RequestParam(value="randNum") String randNum) {
		//获取randList, 和随机数做对比
		int sizeOld = randSet.size();
		randSet.add(randNum);
		int sizeNew = randSet.size();
		if (sizeOld == sizeNew) {
			return "{\"status\":\"multi\"}";
		}
		
		//接受excel表格 , 并进行分析存库
		//String originalFilename = multipartFile.getOriginalFilename();
		//获取文件流
		InputStream inputStream = null;
		try {
			inputStream = multipartFile.getInputStream();
			ParseExcel parseExcel = new ParseExcel();
			List<CourseType> studentScore = parseExcel.readExcelContentForStudentScore(inputStream);
			//进行数据库保存(科目、成绩、成绩项)
			for (CourseType courseType : studentScore) {
				Set<ScoreEntity> scoreEntities = courseType.getScoreEntities();
				for (ScoreEntity scoreEntity : scoreEntities) {
					Set<ScoreItem> items = scoreEntity.getItems();
					ProjectUser projectUser = scoreEntity.getProjectUser();
					//根据准考证号查询学生
					projectUser = projectUserService.findByExamineeNumber(projectUser.getExamineeNumber());
					scoreEntity.setProjectUser(projectUser);
					scoreEntity.setCourseType(courseType);
					for (ScoreItem scoreItem : items) {
						scoreItem.setScoreEntity(scoreEntity);
					}
				}
				courseType.setScoreEntities(scoreEntities);
				courseTypeService.saveOrUpdate(courseType);
			}
			
		} catch (Exception e) {
			// 抛出异常, 做异常控制
			return "{\"status\":\"error\"}";
		}
		return "{\"status\":\"sucess\"}";
	}
	
	/**
	 * @describe 学生成绩列表
	 * @date 2017年5月21日
	 * @author liukang
	 */
	@RequestMapping(value="/scoreList")
	public String scoreCourse(@RequestParam(value="pageNumber", defaultValue="1", required=false) String pageNumber,
			@RequestParam(value="pageSize", defaultValue="10", required=false) String pageSize,
			ScoreEntity scoreEntity, ModelMap modelMap) {
		Page<ScoreEntity> pageByCriterias = entityService.getPageByCriterias(pageNumber, pageSize, scoreEntity);
		modelMap.addAttribute("page", pageByCriterias)
				.addAttribute("searchVal", scoreEntity)
				.addAttribute("number", pageNumber)
				.addAttribute("size", pageSize);
		return "score/singleScoreList";
	}
	
	
	
	/*
	 * //进行数据库保存(科目、成绩、成绩项)
			for (CourseType courseType : studentScore) {
				//1、保存科目表
				CourseType saveOrUpdate01 = courseTypeService.saveOrUpdate(courseType);
				//2、保存成绩实体表
				Set<ScoreEntity> scoreEntities = courseType.getScoreEntities();
				courseType.setScoreEntities(null);
				for (ScoreEntity scoreEntity : scoreEntities) {
					Set<ScoreItem> items = scoreEntity.getItems();
					scoreEntity.setItems(null);
					scoreEntity.setCourseType(saveOrUpdate01);
					ProjectUser projectUser = scoreEntity.getProjectUser();
					//根据准考证号查询学生
					projectUser = projectUserService.findByExamineeNumber(projectUser.getExamineeNumber());
					scoreEntity.setProjectUser(projectUser);
					ScoreEntity saveOrUpdate02 = entityService.saveOrUpdate(scoreEntity);
					
					//3、保存成绩项
					for (ScoreItem scoreItem : items) {
						scoreItem.setScoreEntity(saveOrUpdate02);
						itemService.saveOrUpdate(scoreItem);
					}
				}
				courseType.setScoreEntities(scoreEntities);
			}
	 * 
	 * 
	 * 
	 * 
	 */

}
